package com.cdream.basic.algorithm.leetcode.findElement;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author cdream
 * @date 7/10/21
 */
public class Question981{
    public static void main(String[] args) {
        TimeMap timeMap = new TimeMap();
        timeMap.set("foo","bar",1);
        timeMap.get("foo",1);
        timeMap.get("foo",3);
        timeMap.set("foo","bar2",4);
        timeMap.get("foo",4);
        timeMap.get("foo",5);


    }
}
class TimeMap {
    Map<String, TreeMap<Integer,String>> map;
    /** Initialize your data structure here. */
    public TimeMap() {
        map = new HashMap<String,TreeMap<Integer,String>>();
    }

    public void set(String key, String value, int timestamp) {
        TreeMap<Integer,String> treeMap = map.getOrDefault(key,new TreeMap<>());
        treeMap.put(timestamp,value);
        map.put(key,treeMap);
    }

    public String get(String key, int timestamp) {
        TreeMap<Integer,String> treeMap = map.get(key);
        if(treeMap==null){
            return "";
        }
        Integer res = treeMap.floorKey(timestamp);
        if(res==null){
            return "";
        }
        return treeMap.get(res);
    }
}
